//============================================================================
// Name        : boardMusic.c
// Author      : ma.xw
// Version     : 1.1
// Copyright   : ctrlC
// Description : 《我只在乎你》For TQ2440, date:2012-12-16
//============================================================================

#include "../inc/def.h"
extern void Buzzer_Freq_Set(U32 freq);
extern void Delay(int time);
extern void Uart_Printf(char *fmt,...);
extern void Buzzer_Stop( void );
extern int Uart_GetIntNum_GJ(void);
struct Music_t {
    char c1;
    char c2;
    char* geci;
};

struct Music_t myMus_OnlyCareYou[] = {
    {0x16, 0x4, "如"}, 
    {0x16, 0x4, "果"}, 
    {0x16, 0x4, "没"}, 
    {0x15, 0xC4, "有"}, 
    {0x16, 0x8, "遇"}, 
    {0x16, 0x84, "见"}, 
    {0x6, 0x84, "你,"}, 
    {0x16, 0xA0, 0}, 
    {0x16, 0xC4, "我"}, 
    {0x16, 0xC4, "将"}, 
    {0x16, 0xC4, "会"}, 
    {0x16, 0x84, "是"}, 
    {0x16, 0x8, "在"}, 
    {0x16, 0xC4, "哪"}, 
    {0x6, 0x84, "里?"}, 
    {0x16, 0xA0, 0}, 
    {0x17, 0x44, "日"}, 
    {0x17, 0x44, "子"}, 
    {0x17, 0x44, "过"}, 
    {0x16, 0xC4, "得"}, 
    {0x17, 0x88, "怎"}, 
    {0x17, 0x44, "么"}, 
    {0x6, 0x84, "样?"}, 
    {0x16, 0xA0, 0}, 
    {0x16, 0xC4, "人"}, 
    {0x16, 0xC4, "生"}, 
    {0x16, 0xC4, "是"}, 
    {0x17, 0x44, "否"}, 
    {0x6, 0xC4, "要"}, 
    {0x16, 0x88, 0}, 
    {0x16, 0x4, "珍"}, 
    {0x15, 0xE0, "惜?"}, 
    {0x16, 0x4, "也"}, 
    {0x16, 0x4, "许"}, 
    {0x16, 0x4, "认"}, 
    {0x15, 0xC4, "识"}, 
    {0x16, 0x8, "某"}, 
    {0x16, 0x84, "一"}, 
    {0x6, 0x84, "人,"}, 
    {0x16, 0xA0, 0}, 
    {0x16, 0xC4, "过"}, 
    {0x16, 0xC4, "着"}, 
    {0x16, 0xC4, "平"}, 
    {0x16, 0x84, "凡"}, 
    {0x16, 0x8, "的"}, 
    {0x16, 0x84, "日"}, 
    {0x6, 0xC4, "子,"}, 
    {0x16, 0xA0, 0}, 
    {0x17, 0x44, "不"}, 
    {0x17, 0x48, "知"}, 
    {0x16, 0xC4, "道"}, 
    {0x17, 0x88, "会"}, 
    {0x17, 0x44, "不"}, 
    {0x6, 0x84, "会,"}, 
    {0x16, 0xA0, 0}, 
    {0x16, 0xC4, "也"}, 
    {0x16, 0xC4, "有"}, 
    {0x16, 0xC4, "爱"}, 
    {0x16, 0x84, "情"}, 
    {0x16, 0xC8, "甜"}, 
    {0x17, 0xC8, "如"}, 
    {0x7, 0xA0, "蜜,"}, 
    {0x17, 0x88, 0}, 
    {0x16, 0x88, "任"}, 
    {0x17, 0x48, "时"}, 
    {0x17, 0x88, "光"}, 
    {0x17, 0xC8, "匆"}, 
    {0x17, 0x88, "匆"}, 
    {0x17, 0x48, "流"}, 
    {0x17, 0x8, "去,"}, 
    {0x17, 0x48, "我"}, 
    {0x17, 0x8, "只"}, 
    {0x16, 0xC8, "在"}, 
    {0x16, 0x88, "乎"}, 
    {0x16, 0xE0, "你,"}, 
    {0x10, 0x18, 0}, 
    {0x17, 0x44, "心"}, 
    {0x17, 0x84, "甘"}, 
    {0x17, 0xC8, "情"}, 
    {0x17, 0x88, "愿"}, 
    {0x17, 0x48, "感"}, 
    {0x17, 0x8, "染"}, 
    {0x17, 0x48, "你"}, 
    {0x17, 0x8, "的"}, 
    {0x6, 0x8, "气"}, 
    {0x16, 0x88, 0}, 
    {0x6, 0xD0, "息,"}, 
    {0x16, 0xE0, 0}, 
    {0x10, 0x8, 0}, 
    {0x16, 0xC4, "人"}, 
    {0x16, 0x84, "生"}, 
    {0x16, 0xC8, "几"}, 
    {0x7, 0x48, "何"}, 
    {0x17, 0x50, 0}, 
    {0x10, 0x8, 0}, 
    {0x17, 0x50, "能"}, 
    {0x17, 0x88, "够"}, 
    {0x17, 0xC8, "得"}, 
    {0x17, 0x48, "到"}, 
    {0x7, 0x88, "知"}, 
    {0x17, 0x48, 0}, 
    {0x17, 0x58, "已,"}, 
    {0x16, 0xC4, "失"}, 
    {0x16, 0xC4, "去"}, 
    {0x18, 0x8, "生"}, 
    {0x17, 0xC8, "命"}, 
    {0x17, 0x88, "力"}, 
    {0x17, 0x44, "量"}, 
    {0x8, 0x4, 0}, 
    {0x18, 0x8, 0}, 
    {0x17, 0xC8, "也"}, 
    {0x17, 0x88, "不"}, 
    {0x17, 0x48, "可"}, 
    {0x7, 0x90, "惜,"}, 
    {0x17, 0xA0, 0}, 
    {0x17, 0xC8, "所"}, 
    {0x7, 0x84, "以"}, 
    {0x17, 0x44, 0}, 
    {0x17, 0x50, "我"}, 
    {0x17, 0x48, "求"}, 
    {0x17, 0x88, "求"}, 
    {0x17, 0xD8, "你,"}, 
    {0x17, 0x48, "别"}, 
    {0x18, 0x88, "让"}, 
    {0x18, 0x48, "我"}, 
    {0x7, 0xC4, "离"}, 
    {0x17, 0x84, 0}, 
    {0x17, 0x48, "开"}, 
    {0x16, 0xE0, "你,"}, 
    {0x10, 0x8, 0}, 
    {0x16, 0x88, "除"}, 
    {0x17, 0x48, "了"}, 
    {0x17, 0xC8, "你"}, 
    {0x18, 0x48, "我"}, 
    {0x18, 0x84, "不"}, 
    {0x7, 0xC4, "能"}, 
    {0x17, 0xC4, 0}, 
    {0x17, 0x84, "感"}, 
    {0x17, 0x48, "到"}, 
    {0x16, 0xC8, "一"}, 
    {0x17, 0x48, "丝"}, 
    {0x17, 0x44, "丝"}, 
    {0x7, 0x88, "情"}, 
    {0x17, 0x44, 0}, 
    {0x17, 0x60, "意."}, 
};

struct Music_t myMus_YellowHill[] = {
    {0x15, 0xC8, "我"}, 
    {0x15, 0xC8, "家"}, 
    {0x15, 0xC8, "住"}, 
    {0x15, 0xC8, "在"}, 
    {0x16, 0x88, "黄"}, 
    {0x15, 0xC8, "土"}, 
    {0x16, 0x90, "高"}, 
    {0x6, 0x98, "坡,"}, 
    {0x6, 0x44, 0}, 
    {0x6, 0x4, 0}, 
    {0x15, 0xE0, 0}, 
    {0x16, 0x48, "大"}, 
    {0x16, 0x50, "风"}, 
    {0x16, 0x8, "从"}, 
    {0x15, 0xC8, "坡"}, 
    {0x15, 0xC8, "上"}, 
    {0x15, 0x88, "刮"}, 
    {0x15, 0xF0, "过,"}, 
    {0x10, 0x8, 0}, 
    {0x15, 0xC8, "不"}, 
    {0x15, 0xD0, "管"}, 
    {0x15, 0xC8, "是"}, 
    {0x16, 0x88, "西"}, 
    {0x16, 0x88, "北"}, 
    {0x16, 0x90, "风"}, 
    {0x16, 0x8C, "还"}, 
    {0x16, 0x84, "是"}, 
    {0x15, 0xC8, "东"}, 
    {0x16, 0x88, "南"}, 
    {0x6, 0x8C, "风,"}, 
    {0x7, 0x50, 0}, 
    {0x5, 0x88, 0}, 
    {0x15, 0x48, 0}, 
    {0x15, 0xC, "都"}, 
    {0x14, 0xC4, "是"}, 
    {0x15, 0x8, "我"}, 
    {0x15, 0xC8, "的"}, 
    {0x15, 0xD0, "歌"}, 
    {0x15, 0x8, "我"}, 
    {0x5, 0x44, "的"}, 
    {0x15, 0x10, 0}, 
    {0x14, 0xE0, "歌."}, 
    {0x10, 0x10, 0}, 
    {0x15, 0xC8, "我"}, 
    {0x15, 0xC8, "家"}, 
    {0x15, 0xC8, "住"}, 
    {0x15, 0xC8, "在"}, 
    {0x16, 0x88, "黄"}, 
    {0x15, 0xC8, "土"}, 
    {0x16, 0x90, "高"}, 
    {0x6, 0x98, "坡,"}, 
    {0x6, 0x44, 0}, 
    {0x6, 0x4, 0}, 
    {0x15, 0xE0, 0}, 
    {0x16, 0x48, "大"}, 
    {0x16, 0x50, "风"}, 
    {0x16, 0x8, "从"}, 
    {0x15, 0xC8, "坡"}, 
    {0x15, 0xC8, "上"}, 
    {0x15, 0x88, "刮"}, 
    {0x15, 0xE0, "过,"}, 
    {0x10, 0x20, 0}, 
    {0x15, 0xC8, "不"}, 
    {0x15, 0xD0, "管"}, 
    {0x15, 0xC8, "是"}, 
    {0x16, 0x88, "西"}, 
    {0x16, 0x88, "北"}, 
    {0x16, 0x90, "风"}, 
    {0x16, 0x8C, "还"}, 
    {0x16, 0x84, "是"}, 
    {0x15, 0xC8, "东"}, 
    {0x16, 0x88, "南"}, 
    {0x6, 0x8C, "风,"}, 
    {0x7, 0x50, 0}, 
    {0x5, 0x88, 0}, 
    {0x15, 0x48, 0}, 
    {0x15, 0xC, "都"}, 
    {0x14, 0xC4, "是"}, 
    {0x15, 0x8, "我"}, 
    {0x15, 0xC8, "的"}, 
    {0x15, 0xD0, "歌"}, 
    {0x15, 0x8, "我"}, 
    {0x5, 0x44, "的"}, 
    {0x15, 0x10, 0}, 
    {0x14, 0xE0, "歌."}, 
    {0x10, 0x20, 0}, 
    {0x16, 0x48, "不"}, 
    {0x16, 0x50, "管"}, 
    {0x16, 0x10, "过"}, 
    {0x15, 0xC8, "去"}, 
    {0x10, 0x8, 0}, 
    {0x15, 0x88, "了"}, 
    {0x16, 0x4C, "多"}, 
    {0x16, 0x44, "少"}, 
    {0x6, 0x48, "岁"}, 
    {0x16, 0x8, 0}, 
    {0x15, 0xE0, "月,"}, 
    {0x16, 0x4C, "祖"}, 
    {0x16, 0x44, "祖"}, 
    {0x16, 0x48, "辈"}, 
    {0x16, 0x8, "辈"}, 
    {0x5, 0xCC, "留"}, 
    {0x14, 0xC4, 0}, 
    {0x14, 0xD0, "下"}, 
    {0x15, 0x30, "我,"}, 
    {0x10, 0x10, 0}, 
    {0x16, 0x48, "留"}, 
    {0x16, 0x50, "下"}, 
    {0x16, 0x8, "我"}, 
    {0x15, 0xC8, "一"}, 
    {0x15, 0xC8, "望"}, 
    {0x15, 0x88, "无"}, 
    {0x15, 0xC8, "际"}, 
    {0x16, 0x50, "唱"}, 
    {0x6, 0x48, "着"}, 
    {0x16, 0x8, 0}, 
    {0x15, 0xE0, "歌,"}, 
    {0x15, 0xCC, "还"}, 
    {0x15, 0xC4, "有"}, 
    {0x15, 0xC8, "身"}, 
    {0x16, 0x48, "边"}, 
    {0x15, 0xC8, "这"}, 
    {0x15, 0x88, "条"}, 
    {0x5, 0x48, "黄"}, 
    {0x14, 0xD0, 0}, 
    {0x15, 0xB0, "河"}, 
    {0x5, 0xD0, "噢"}, 
    {0x6, 0x60, 0}, 
    {0x6, 0xCC, 0}, 
    {0x7, 0x44, 0}, 
    {0x6, 0xC8, 0}, 
    {0x16, 0xB0, 0}, 
    {0x10, 0x8, 0}, 
    {0x17, 0x48, "我"}, 
    {0x17, 0x48, "家"}, 
    {0x17, 0x48, "住"}, 
    {0x17, 0x8, "在"}, 
    {0x16, 0xC8, "黄"}, 
    {0x16, 0x88, "土"}, 
    {0x16, 0xD0, "高"}, 
    {0x6, 0xCC, "坡,"}, 
    {0x7, 0x44, 0}, 
    {0x6, 0x58, 0}, 
    {0x6, 0x8, 0}, 
    {0x15, 0xD0, 0}, 
    {0x17, 0x48, "四"}, 
    {0x17, 0x48, "季"}, 
    {0x17, 0x48, "风"}, 
    {0x17, 0x8, "从"}, 
    {0x16, 0xC8, "坡"}, 
    {0x16, 0x88, "上"}, 
    {0x16, 0xD0, "刮"}, 
    {0x6, 0xC8, "过,"}, 
    {0x7, 0x44, 0}, 
    {0x6, 0x58, 0}, 
    {0x6, 0x8, 0}, 
    {0x15, 0xD0, 0}, 
    {0x15, 0xC8, "不"}, 
    {0x15, 0xD0, "管"}, 
    {0x15, 0xC8, "是"}, 
    {0x16, 0x90, "八"}, 
    {0x16, 0x8C, "百"}, 
    {0x16, 0x84, "年"}, 
    {0x16, 0x8C, "还"}, 
    {0x16, 0x84, "是"}, 
    {0x15, 0xC8, "一"}, 
    {0x16, 0x88, "万"}, 
    {0x6, 0x8C, "年,"}, 
    {0x7, 0x44, 0}, 
    {0x5, 0x88, 0}, 
    {0x15, 0x48, 0}, 
    {0x15, 0xC, "都"}, 
    {0x14, 0xC4, "是"}, 
    {0x15, 0x8, "我"}, 
    {0x15, 0xC8, "的"}, 
    {0x15, 0xD0, "歌"}, 
    {0x15, 0x8, "我"}, 
    {0x5, 0x44, "的"}, 
    {0x15, 0x4, 0}, 
    {0x16, 0xB0, "歌."}, 
    {0x10, 0x10, 0}, 
    {0x5, 0x10, "噢"}, 
    {0x15, 0xB0, 0}, 
    {0x5, 0xD0, "噢~"}, 
    {0x6, 0x60, 0}, 
    {0x6, 0xCC, 0}, 
    {0x7, 0x44, 0}, 
    {0x6, 0xC8, 0}, 
    {0x16, 0xE0, 0}, 
    {0x10, 0x10, 0}, 
};

struct Music_t myMus_HappySlave[] = {
    {0x15, 0x10, "太"}, 
    {0x5, 0xC8, "阳"}, 
    {0x16, 0x4, 0}, 
    {0x5, 0x84, "阿!"}, 
    {0x15, 0xE0, 0}, 
    {0x16, 0xC, "霞"}, 
    {0x16, 0x84, "光"}, 
    {0x6, 0xD0, "万"}, 
    {0x7, 0x44, 0}, 
    {0x6, 0x84, 0}, 
    {0x16, 0xC4, 0}, 
    {0x16, 0x20, "丈,"}, 
    {0x16, 0x90, "雄"}, 
    {0x5, 0x8, "鹰"}, 
    {0x5, 0x84, 0}, 
    {0x16, 0x4, 0}, 
    {0x5, 0xCC, "啊,"}, 
    {0x16, 0x8, 0}, 
    {0x15, 0x8C, "展"}, 
    {0x15, 0xC4, "翅"}, 
    {0x5, 0x8, 0}, 
    {0x14, 0xC8, "飞"}, 
    {0x15, 0x20, "翔."}, 
    {0x16, 0xD8, "高"}, 
    {0x16, 0xC8, "原"}, 
    {0x6, 0x88, "春"}, 
    {0x6, 0xC4, 0}, 
    {0x16, 0x84, 0}, 
    {0x16, 0x10, "光"}, 
    {0x17, 0x10, "无"}, 
    {0x6, 0xC4, "限"}, 
    {0x7, 0x4, 0}, 
    {0x6, 0x84, 0}, 
    {0x16, 0xC4, 0}, 
    {0x6, 0x18, "好,"}, 
    {0x16, 0x88, 0}, 
    {0x15, 0xCC, "叫"}, 
    {0x16, 0x4, "我"}, 
    {0x16, 0x88, "怎"}, 
    {0x16, 0xC8, "能"}, 
    {0x6, 0x8, "不"}, 
    {0x16, 0x88, 0}, 
    {0x5, 0x8, "歌"}, 
    {0x15, 0x88, 0}, 
    {0x15, 0xE0, "唱."}, 
    {0x16, 0xD8, "高"}, 
    {0x16, 0xC8, "原"}, 
    {0x6, 0x88, "春"}, 
    {0x6, 0xC4, 0}, 
    {0x16, 0x84, 0}, 
    {0x16, 0x10, "光"}, 
    {0x17, 0x8, "无"}, 
    {0x6, 0xC4, "限"}, 
    {0x7, 0x4, 0}, 
    {0x6, 0x84, 0}, 
    {0x16, 0xC4, 0}, 
    {0x6, 0x18, "好,"}, 
    {0x16, 0x88, 0}, 
    {0x15, 0xCC, "叫"}, 
    {0x16, 0x4, "我"}, 
    {0x16, 0x88, "怎"}, 
    {0x16, 0xC8, "能"}, 
    {0x6, 0x8, "不"}, 
    {0x5, 0xC4, 0}, 
    {0x15, 0x84, 0}, 
    {0x5, 0x8, "歌"}, 
    {0x14, 0xC8, 0}, 
    {0x15, 0x20, "唱."}, 
};


U32 freqTab[] = {
        0,//静止符
        200, 200, 200, 200, 200, 200, 200, //低二 1,2,3,4,5,6,7
        131, 147, 165, 175, 196, 220, 247, //低一 1,2,3,4,5,6,7
        262, 286, 330, 349, 392, 440, 494, //常音 1,2,3,4,5,6,7
        523, 587, 659, 698, 784, 879, 987, //高一 1,2,3,4,5,6,7
        1047, 1175, 1319, 1397, 1568, 1760, 1976, //高二 1,2,3,4,5,6,7
};

//BOARD BEEP
void mybeep(U32 freq, U32 ms) {
    Buzzer_Freq_Set(freq);
    Delay(ms);
    Buzzer_Stop();
}

//shichangBase_Ms 表示1/64拍所用的毫秒数
void subPlay(char yingaoidx, char shichang, int kongxi, int shichangBase_Ms) {
    if (0 != yingaoidx) { //如果yingaoidx为0则表示静止符
        //real play 以指定时间长度,指定频率PWM播放
        mybeep(freqTab[yingaoidx], shichang * shichangBase_Ms);
        if (kongxi) {
            Delay(15);
        }
    } else {
        Buzzer_Stop();
        Delay(shichang * shichangBase_Ms);
    }
    return;
}

void print_str_hex(char* s) {
    int j = 0;
    Uart_Printf("(");
    while(s[j]) {
        Uart_Printf("0x%02X ", s[j]);
        j++;
    }
    Uart_Printf(")");
}

int play(struct Music_t* pmusHead, int eleCnt) {
    int idx = 0;
    while(idx < eleCnt) {
        int isum = ((pmusHead[idx].c1 << 8) | pmusHead[idx].c2);
        char yingaoIdx = ((isum >> 6) & 0x3F);
        char shichang = (isum & 0x3F);
        int kongxi = ((isum >> 12) & 0x1);
        
        if() {
            
        }
        if (0 != pmusHead[idx].geci) {
            Uart_Printf("yingaoIdx=%d, shichang=%d, kongxi=%d, geci=%s",yingaoIdx, shichang, kongxi, pmusHead[idx].geci);
            print_str_hex(pmusHead[idx].geci);
            Uart_Printf("\n");
        } else {
            Uart_Printf("yingaoIdx=%d, shichang=%d, kongxi=%d, geci=%s\n",yingaoIdx, shichang, kongxi, "null");
        }
        subPlay(yingaoIdx, shichang, kongxi, 40);
        ++idx;
    }
    return 0;
}

struct MusHead_t {
	struct Music_t* head;
	int             toneCnt;
	char*           name;
};

struct MusHead_t allMus[] = {
	{myMus_OnlyCareYou, sizeof(myMus_OnlyCareYou) / sizeof(struct Music_t), "我只在乎你"},
	{myMus_YellowHill, sizeof(myMus_YellowHill) / sizeof(struct Music_t), "黄土高坡"},
	{myMus_HappySlave, sizeof(myMus_HappySlave) / sizeof(struct Music_t), "翻身农奴把歌唱"},
};

void musicGo(void) {
	while(1)
	{
		U8 idx;
		int i = 0;int j = 0;
		int cnt = sizeof(allMus) / sizeof(struct MusHead_t);
		Uart_Printf("\nPlease select music : \n");	
		for(i=0; i < cnt; i++) {
			Uart_Printf("%2d : %s", i, allMus[i].name);
            print_str_hex(allMus[i].name);
            Uart_Printf("\n");
		}
		idx = Uart_GetIntNum_GJ() ;	
		if(idx < cnt) {
			play(allMus[idx].head, allMus[idx].toneCnt);
		} else {
			Uart_Printf("wrong num!\n");
		}
	}
    
}
